#include "../include/DBMS.h"
//#include "index.cpp"

DBMS::DBMS()
{

    ReadFromFile();

};

DBMS::~DBMS()
{
    list<Table>::iterator it;
    for(it = db.begin(); it != db.end(); it++)
        (*it).DeleteTable();
    //release table memory.
};

void DBMS::CreateTable(Create_Stmt CreateData)
{
    db.push_back(Table(CreateData));
};

void DBMS::Insert(Insert_Stmt InsertData)
{
    list<Table>::iterator it;
    for(it = db.begin(); it != db.end(); it++)
    {
        if((*it).GetName() == InsertData.table)
        {
            (*it).Insert(InsertData);
            break;
        }
    }
    if (it  == db.end())
    {
        cerr<<InsertData.table<<":The table is not exist."<<endl;
        exit(0);
    }
};

void DBMS::Print(ofstream &Output)
// todo :print attribute type
{
    list<Table>::iterator it;
    for(it = db.begin(); it != db.end(); it++)
        (*it).Print(Output);

};
void DBMS::WriteToFile()
{
    list<Table>::iterator it;

    //c++ style:
    ofstream fout("file.dat", ios::binary);
    int ll = db.size();
    fout.write((char*)&ll,IBYTES);
    for(it = db.begin(); it != db.end(); it++)
        (*it).Write(fout);
    fout.close();

    //c style:
    /*
    FILE *fp;
    fp=fopen("file.dat","wb");
    if(fp==0){puts("write error");exit(0);}
    int ll = db.size();
    fwrite(&ll,IBYTES,1,fp);
     for(it = db.begin(); it != db.end(); it++)
        (*it).Write(fp);
    fclose(fp);*/

};
void DBMS::ReadFromFile()
{
    ifstream fin("file.dat", ios::binary);
    if(fin)
    {
        //cout<<"file open success"<<endl;
        int dbsize ;
        fin.read((char*)&dbsize,IBYTES);
        for(int i=0; i<dbsize; i++)
        {

            Table tb;
            tb.Read(fin);
            db.push_back(tb);
        }
    }
    fin.close();
    /*
    FILE *fp;
    fp=fopen("file.dat","rb");
    if(fp==0){puts("read none");return;}
    int dbsize =0;
    fread(&dbsize,IBYTES,1,fp);
    for(int i=0;i<dbsize;i++)
    {
        Table tb;
        tb.Read(fp);
        db.push_back(tb);
    }
    fclose(fp);*/
};

void DBMS::Select(Select_Stmt SelectData)
{
    list<string> TableName = SelectData.TableName;
    list<string> AttrName =SelectData.AttrName;
    map<string,string>  originname =SelectData.originname;
    list<int> Relation=SelectData.Relation;
    list<Condition_Stmt*> WhereCond =  SelectData.WhereCond;

    list<string> ::iterator it1;//tablename
    list<string>::iterator it2;//attrobutename
    list<Table>::iterator it3;//db
    cmatch what;
    regex reg_split("([a-z0-9_]+)\\.([a-z0-9_*]+)",regex::icase);

    list<Table*> tlist;
    list<Table*>::iterator tit;

    vector<vector<void*> > sdata;
    vector<int> st;
    int ssize[2];

    int attrtype[MAX*2];
    int i,j=0,k=0;
    //select form part:

    //find : "from table "first:
    for(it1=TableName.begin(); it1!=TableName.end(); it1++)
    {
        for(it3 = db.begin(); it3 != db.end(); it3++)
        {
            if( (*it3).GetName() == originname[(*it1)] || (*it3).GetName() == *it1 )
                tlist.push_back(&(*it3));
        }
    }

    ofstream sout("select.result");
    //select attribute from table part
    it2 = AttrName.begin();
    sout<<"|";

    if(*it2  == "*") //selecet *
    {
        int ix = 0;
        st.push_back(0);
        for(tit =tlist.begin(); tit!=tlist.end(); tit++)
        {
            string* tmpstr = (*(*tit)).GetAttributeName();
            int* tmptype =(*(*tit)).GetType();
            vector<void*>* data = (*(*tit)).GetTableData() ;
            //list<Sub_Stmt*>* dd;
            //dd = new list<Sub_Stmt*>[MAX];
            ssize[ix++]  = (*(*tit)).GetSize();

            for(i=0; i<(*(*tit)).GetSize(); i++)
            {
                sdata.push_back(data[i]);

                attrtype[j++]=tmptype[i];
                sout<<tmpstr[i]<<"|";
            }
        }
    }
    else
    {
        int over=-1;
        int ix = 0;
        int tt;
        for(tit = tlist.begin(),tt = 0; tit!=tlist.end(); tit++,tt++)
        {
            over++;
            string* tmpstr = (*(*tit)).GetAttributeName();
            int* tmptype =(*(*tit)).GetType();
            vector<void*>* data = (*(*tit)).GetTableData() ;
            //vector<void*>* dd;
            //dd = new vector<void*>[MAX];
            j=0;
            int isstar=false;
            for(it2 =AttrName.begin(); it2 !=AttrName.end(); it2++)
            {

                if(regex_match((*it2).c_str(),what,reg_split))//a.b fomat
                {
                    if( what[1] == (*(*tit)).GetName() || originname[what[1]]==  (*(*tit)).GetName() )
                    {

                        if(what[2]=="*")
                        {
                            st.push_back(0);
                            for(i=0; i<(*(*tit)).GetSize(); i++)
                            {
                                //dd[i]=data[i];
                                sdata.push_back(data[i]);
                                attrtype[k++]=tmptype[i];
                                sout <<tmpstr[i]<<"|";
                                j++;
                            }
                            isstar=true;
                            break;
                        }
                        else
                        {
                            for(i=0; i<(*(*tit)).GetSize(); i++)
                            {
                                if(what[2] == tmpstr[i])
                                {
                                    st.push_back(tt);
                                    //dd[j++]=data[i];
                                    sdata.push_back(data[i]);
                                    attrtype[k++]=tmptype[i];
                                    sout <<tmpstr[i]<<"|";
                                    j++;
                                    break;
                                }
                            }
                        }
                    }
                }
                else
                {
                    for(i=0; i<(*(*tit)).GetSize(); i++)
                    {

                        if(*it2 == tmpstr[i])
                        {
                            st.push_back(tt);
                            sdata.push_back(data[i]);
                            attrtype[k++]=tmptype[i];
                            sout <<tmpstr[i]<<"|";
                            j++;
                            break;
                        }

                    }
                }
            }
            //sdata.push_back(dd);
            ssize[ix++]=j;
            if(isstar)
                j=1;
            over=over+j;
        }
        if(SelectData.SelectType >=1)
            over--;
        if((unsigned)over>AttrName.size())
        {
            cerr<<"the attribute is ambiguous"<<"\n";
            exit(0);
        }
    }

    sout<<endl;
    //where part:
    vector<vector<int>*> slist;
    int ttmp,atmp,c;
    Table* tptr;
    vector<int> sflag;
    list<Condition_Stmt*>::iterator it4;
    //if it is inner join , it will be campare later
    list<Condition_Stmt*>::iterator cmplater;
    int islater=false;
    vector<void*> c1later;
    int typelater;
    int alater,tlater;
    Table* tptrlater;
    vector<void*> cmpdata1;
    int catype1;
    int catype2;
    vector<void*> cmpdata2;
    Sub_Stmt* constva;
    int symbol;
    //void* cmpval;

    for (it4 = WhereCond.begin(); it4!=WhereCond.end(); it4++)
    {

        if((*it4)->cond1.table !="")
        {
            for(tit = tlist.begin(),c=0; tit!=tlist.end(); tit++,c++)
            {

                if( (*(*tit)).GetName() == (*it4)->cond1.table || (*(*tit)).GetName() == originname[(*it4)->cond1.table])
                {

                    string* tmpstr = (*(*tit)).GetAttributeName();
                    int* tmptype =(*(*tit)).GetType();
                    vector<void*>* data = (*(*tit)).GetTableData() ;


                    for(i=0; i<(*(*tit)).GetSize(); i++)
                    {
                        if(tmpstr[i] == (*it4)->cond1.attr)
                        {
                            catype1 = tmptype[i];
                            cmpdata1 = data[i];
                            ttmp = c;
                            tptr = (*tit);
                            atmp =i;
                            break;
                            //wcond.cmb1= data[i].begin();
                            //tit = tlist.end();//for break;
                        }
                    }
                }
            }
        }
        else
        {
            for(tit = tlist.begin(),c=0; tit!=tlist.end(); tit++,c++)
            {
                string* tmpstr = (*(*tit)).GetAttributeName();
                int* tmptype =(*(*tit)).GetType();
                vector<void*>* data = (*(*tit)).GetTableData() ;
                for(i=0; i<(*(*tit)).GetSize(); i++)
                {
                    if(tmpstr[i] == (*it4)->cond1.attr)
                    {
                        ttmp = c;
                        atmp = i;
                        tptr = (*tit);
                        catype1 = tmptype[i];
                        cmpdata1= data[i];
                        break;
                    }
                }
            }
        }

        symbol = (*it4)->symbol;
        int tt = (*it4)->cond2->type;
        if(tt == ALIAS)
        {
            cmplater = it4;
            c1later = cmpdata1;
            tlater = ttmp;
            tptrlater = tptr;
            alater = atmp ;
            //symlater = symbol;
            typelater = catype1;
            islater = true;
            continue;
        }
        else
        {
            constva = (*it4)->cond2;
            if(catype1 + (*(constva)).type !=5)
            {
                cerr<<"  the attribute type is wrong.."<<endl;
                exit(0);
            }
            void* val = 0;
            if (catype1 ==0)
                val = &(((String_Stmt*)(constva))->str);
            if (catype1 ==1)
                val =  &(((Number_Stmt*)(constva))->num);
            sflag.push_back(ttmp);

            // vector<int>* re = IndexFind(tptr,atmp,catype1,val,symbol);

            //if((*re).front() ==-1)
            int ma =0;
            int mi =0;
            if(catype1 == 1)
            {
                ma = tptr->GetMax(atmp);
                mi = tptr ->GetMin(atmp);
                //if(*(int*(val))
            }
            vector<int>* re = SimpleFind(cmpdata1,catype1,val,symbol,ma,mi);
            slist.push_back(re);
        }
    }

    int ct=0;
    int sum =0;
    int r =0;
    if(sflag.size()>1)
    {
        list<int>::iterator rit;
        for(rit = SelectData.Relation.begin(); rit!= SelectData.Relation.end(); rit++)
        {
            if(sflag[r]==sflag[r+1])
            {
                if(*rit == 0)
                {
                    vector<int>* tmp = AndOp(slist[0],slist[1]);
                    slist.clear();
                    slist.push_back(tmp);
                }
                else
                {
                    vector<int>* tmp = OrOp(slist[0],slist[1]);
                    slist.clear();
                    slist.push_back(tmp);
                }
            }
        }
    }
    if(tlist.size()>1)
    {
        if(islater) //alias
        {
            /*
            cmplater : wherecond's iterator
            c1later  : cmpdata1
            tlater: tptr
            alater: atmp
            typelater: cmpdata1's type
             */
            symbol = (*cmplater)->symbol;

            if(((Alias_Stmt*)((*cmplater)->cond2))->table !="")
            {

                for(tit = tlist.begin(),c=0; tit!=tlist.end(); tit++,c++)
                {
                    if( (*(*tit)).GetName() == ((Alias_Stmt*)((*cmplater)->cond2))->table )//|| (*(*tit)).GetName() == originname[((Alias_Stmt*)((*cmplater)->cond2))->table])
                    {

                        string* tmpstr = (*(*tit)).GetAttributeName();
                        int* tmptype =(*(*tit)).GetType();
                        vector<void*>* data = (*(*tit)).GetTableData() ;
                        for(i=0; i<(*(*tit)).GetSize(); i++)
                        {

                            if(tmpstr[i] == ((Alias_Stmt*)((*cmplater)->cond2))->attr)
                            {
                                catype2 = tmptype[i];
                                cmpdata2 = data[i];
                                ttmp = c;
                                tptr = (*tit);
                                atmp = i;
                                break;
                            }
                        }
                    }
                }
            }
            else
            {
                for(tit = tlist.begin(); tit!=tlist.end(); tit++)
                {
                    string* tmpstr = (*(*tit)).GetAttributeName();
                    int* tmptype =(*(*tit)).GetType();
                    vector<void*>* data = (*(*tit)).GetTableData() ;
                    for(i=0; i<(*(*tit)).GetSize(); i++)
                    {
                        if(tmpstr[i] ==((Alias_Stmt*)((*cmplater)->cond2))->attr)
                        {
                            ttmp = c;
                            tptr = (*tit);
                            atmp = i;
                            catype2 = tmptype[i];
                            cmpdata2= data[i];
                            break;
                        }
                    }
                }
            }
            if(typelater != catype2)
            {
                cerr<<" the attribute are of different types and cannot be compared."<<endl;
                exit(0);
            }
        }

        if(slist.size() ==0)
        {
            if(islater)
            {
                Table*  t;
                int  a;
                int type ;
                vector<void*> cmp1,cmp2;
                t = tptrlater;
                a = alater;
                cmp1 = c1later;
                cmp2 = cmpdata2;
                type = typelater;
                Chash_int* in =0;
                if(catype1 ==1)
                {
                    in = IndexFindInt(t,a);
                }
                for(int h=0; h<(signed)cmp1.size(); h++)
                {
                    void* val = cmp1[h];
                    vector<int>* re;

                    int ma =0;
                    int mi =0;
                    if(catype1 == 1)
                    {
                        ma = t->GetMax(a);
                        mi = t->GetMin(a);
                        //if(*(int*(val))
                        if(*((int*)val) <= ma && *((int*)val) >= mi)
                        {

                            if(in)
                            {
                                re = in->search(*((int*)val));
                            }
                            else
                                re = SimpleFind(cmp1,type,val,symbol,ma,mi);
                        }
                    }
                    vector<int>::iterator lit;
                    if(SelectData.SelectType == 1)
                    {
                        ct = ct + (*re).size();
                        continue;
                    }
                    if(SelectData.SelectType == 2)
                    {
                        if(tlater==st[0])
                            sum = sum + (*((int*)sdata[0][h]))*(cmp2.size());
                        else
                        {
                            for(lit = (*re).begin(); lit!=(*re).end(); lit++)
                            sum =sum + (*((int*)sdata[0][*lit]));
                        }
                        continue;
                    }
                    for(lit = (*re).begin(); lit!=(*re).end(); lit++)
                    {
                        int f1 =h,f2=*lit;
                        if(tlater!= st[0])
                        {
                            f1 = *lit;
                            f2 = h;
                        }
                        for(i=0; i<ssize[0]; i++)
                        {
                            if(attrtype[i] == 0)
                            {
                                if(sdata[i][f1] == NULL)
                                    sout<<"NULL"<<" | ";
                                else
                                    sout<< *((string*)sdata[i][f1])<<" | ";
                            }
                            else
                            {
                                if(sdata[i][f1] == NULL)
                                    sout<<"NULL"<<" | ";
                                else
                                    sout<< *((int*)sdata[i][f1])<<" | ";
                            }
                        }

                        for(i=ssize[0]; i<ssize[0]+ssize[1]; i++)
                        {

                            if(attrtype[i] == 0)
                            {
                                if(sdata[i][f2] == NULL)
                                    sout<<"NULL"<<" | ";
                                else
                                    sout<< *((string*)sdata[i][f2])<<" | ";
                            }
                            else
                            {
                                if(sdata[i][f2] == NULL)
                                    sout<<"NULL"<<" | ";
                                else
                                    sout<< *((int*)sdata[i][f2])<<" | ";
                            }
                        }
                        sout<<endl;
                    }
                }
            }
        }
        if(slist.size() ==1)
        {
            vector<int>* sl;
            //list<int>* sln;
            int sf;
            if(islater)
            {
                Table*  t;
                int  a;
                int type ;
                vector<void*> cmp1,cmp2;
                sl =  slist[0];
                sf =  sflag[0];
                if(tlater == sf)
                {
                    t = tptr;
                    a = atmp;
                    cmp1 = cmpdata2;
                    cmp2 = c1later;
                    type = catype2;
                }
                else
                {
                    t = tptrlater;
                    a = alater;
                    cmp1 = c1later;
                    cmp2 = cmpdata2;
                    type = typelater;
                }
                //int  sls = (*sl).size();
                Chash_int* in =0;
                if(catype1 ==1)
                {
                    in = IndexFindInt(t,a);
                }

                int rela = SelectData.Relation.front();
                if(rela == 0)
                {
                    for(vector<int>::iterator iter =(*sl).begin(); iter!= (*sl).end(); iter++)
                    {
                        void* val = cmp2[*iter];
                        vector<int>* re;
                        int ma =0;
                        int mi =0;
                        if(catype1 == 1)
                        {
                            ma = t->GetMax(a);
                            mi = t->GetMin(a);
                            if(*((int*)val) <= ma && *((int*)val) >= mi)
                            {

                                if(in)
                                {
                                    re = in->search(*((int*)val));
                                }
                                else
                                    re = SimpleFind(cmp1,type,val,symbol,ma,mi);
                            }
                        }
                        vector<int>::iterator lit;
                        if(SelectData.SelectType == 1)
                        {
                            ct = ct + (*re).size();
                            continue;
                        }

                        if(SelectData.SelectType==2)
                        {
                              if(sf == st[0] )
                                    sum = sum + (*((int*)sdata[0][*iter]))*((*re).size());
                               else
                               {
                                   for(lit = (*re).begin(); lit!=(*re).end(); lit++)
                                        sum = sum + (*((int*)sdata[0][*lit]));
                               }
                            continue;
                        }
                        for(lit = (*re).begin(); lit!=(*re).end(); lit++)
                        {
                            int f1=*iter,f2=*lit;
                            for(i=0; i<ssize[0]; i++)
                            {
                                if(sf != st[0])
                                    {
                                        f1= *lit;
                                        f2 =*iter;
                                    }
                                if(attrtype[i] == 0)
                                {
                                    if(sdata[i][f1] == NULL)
                                        sout<<"NULL"<<" | ";
                                    else
                                        sout<< *((string*)sdata[i][f1])<<" | ";
                                }
                                else
                                {
                                    if(sdata[i][f1] == NULL)
                                        sout<<"NULL"<<" | ";
                                    else
                                        sout<< *((int*)sdata[i][f1])<<" | ";
                                }
                            }

                            for(i=ssize[0]; i<ssize[0]+ssize[1]; i++)
                            {

                                if(attrtype[i] == 0)
                                {
                                    if(sdata[i][f2] == NULL)
                                        sout<<"NULL"<<" | ";
                                    else
                                        sout<< *((string*)sdata[i][f2])<<" | ";
                                }
                                else
                                {
                                    if(sdata[i][f2] == NULL)
                                        sout<<"NULL"<<" | ";
                                    else
                                        sout<< *((int*)sdata[i][f2])<<" | ";
                                }
                            }
                            sout<<endl;
                        }
                    }
                }
                if(rela == 1)
                {
                    int  clen = (cmp2).size();
                    int ol = 0;
                    for(int q =0; q<clen; q++)
                    {
                        vector<int>* re = 0;
                        int ma =0;
                        int mi =0;
                        if((*slist[0])[ol] != q &&catype1 == 1)
                        {
                            void* val = cmp2[q];
                            ma = t->GetMax(a);
                            mi = t->GetMin(a);

                            if(*((int*)val) <= ma && *((int*)val) >= mi)
                            {

                                if(in)
                                {
                                    re = in->search(*((int*)val));
                                }
                                else
                                    re = SimpleFind(cmp1,type,val,symbol,ma,mi);
                            }
                        }
                        vector<int>::iterator lit;
                        if(SelectData.SelectType == 1)
                        {
                            if((*slist[0])[ol] == q)
                            {
                                ol++;
                                ct = ct + cmp1.size();
                            }
                            else
                                ct = ct + (*re).size();
                            continue;
                        }
                        if(SelectData.SelectType==2)
                        {
                            if((*slist[0])[ol] == q)
                            {
                                if(sf == st[0] )
                                {
                                    sum = sum + (*((int*)sdata[0][q]))*((cmp1).size());
                                }
                                else
                                {
                                    int len1 = (cmp1).size();
                                    for(int w1=0;w1<len1;w1++)
                                         sum = sum + (*((int*)sdata[0][w1]));
                                }
                                ol++;
                            }
                            else
                            {
                               if(sf == st[0] )
                               {
                                   if(((*re).size())==0)
                                   {
                                       cout<<q<<endl;
                                   }
                                   sum = sum + (*((int*)sdata[0][q]))*((*re).size());
                               }
                               else
                               {
                                   for(lit = (*re).begin(); lit!=(*re).end(); lit++)
                                        sum = sum + (*((int*)sdata[0][*lit]));
                               }
                            }
                            continue;
                        }
                        for(lit = (*re).begin(); lit!=(*re).end(); lit++)
                        {
                            int f1=q,f2=*lit;
                            for(i=0; i<ssize[0]; i++)
                            {
                                if(sf != st[0])
                                    {
                                        f1= *lit;
                                        f2 =q;
                                    }
                                if(attrtype[i] == 0)
                                {
                                    if(sdata[i][f1] == NULL)
                                        sout<<"NULL"<<" | ";
                                    else
                                        sout<< *((string*)sdata[i][f1])<<" | ";
                                }
                                else
                                {
                                    if(sdata[i][f1] == NULL)
                                        sout<<"NULL"<<" | ";
                                    else
                                        sout<< *((int*)sdata[i][f1])<<" | ";
                                }
                            }

                            for(i=ssize[0]; i<ssize[0]+ssize[1]; i++)
                            {

                                if(attrtype[i] == 0)
                                {
                                    if(sdata[i][f2] == NULL)
                                        sout<<"NULL"<<" | ";
                                    else
                                        sout<< *((string*)sdata[i][f2])<<" | ";
                                }
                                else
                                {
                                    if(sdata[i][f2] == NULL)
                                        sout<<"NULL"<<" | ";
                                    else
                                        sout<< *((int*)sdata[i][f2])<<" | ";
                                }
                            }
                            sout<<endl;
                        }
                    }
                }
            }
        }
        if(slist.size() ==2)
        {
            vector<int> sl1 = (*slist[0]);
            vector<int> sl2 = (*slist[1]);
            if(SelectData.SelectType == 1)
            {
                cout<<"count"<<(sl1.size())*(sl1.size())<<"\n";
                return;
            }
            if(SelectData.SelectType==2)
            {
                if(sflag[0]== st[0])
                {
                    for(vector<int>::iterator iter1 = sl1.begin(); iter1!= sl1.end(); iter1++ )
                    sum = sum + (*((int*)sdata[0][*iter1]))*((sl2).size());
                }
                else
                {
                    for(vector<int>::iterator iter1 = sl2.begin(); iter1!= sl2.end(); iter1++ )
                    sum = sum + (*((int*)sdata[0][*iter1]))*((sl2).size());
                }
                cout<<"sum:"<<sum<<"\n";
                return;
            }

            for(vector<int>::iterator iter1 = sl1.begin(); iter1!= sl1.end(); iter1++ )
            {
                for(vector<int>::iterator iter2 = sl2.begin(); iter2!= sl2.end(); iter2++ )
                {
                    int f1 =*iter1,f2=*iter2;
                    if(sflag[0]!= st[0])
                    {
                        f1 = *iter2;
                        f2 = *iter1;
                    }
                    for(i=0; i<ssize[0]; i++)
                    {
                        if(attrtype[i] == 0)
                        {
                            if(sdata[i][f1] == NULL)
                                sout<<"NULL"<<" | ";
                            else
                                sout<< *((string*)sdata[i][f1])<<" | ";
                        }
                        else
                        {
                            if(sdata[i][f1] == NULL)
                                sout<<"NULL"<<" | ";
                            else
                                sout<< *((int*)sdata[i][f1])<<" | ";
                        }
                    }
                    for(i=ssize[0]; i<ssize[0]+ssize[1]; i++)
                    {
                        if(attrtype[i] == 0)
                        {
                            if(sdata[i][f2] == NULL)
                                sout<<"NULL"<<" | ";
                            else
                                sout<< *((string*)sdata[i][f2])<<" | ";
                        }
                        else
                        {
                            if(sdata[i][f2] == NULL)
                                sout<<"NULL"<<" | ";
                            else
                                sout<< *((int*)sdata[i][f2])<<" | ";
                        }
                    }
                }
            }
            sout<<endl;
        }
    }
    else
    {
        //only one:
        if(slist.size()>0)
        {
            ct = (*slist[0]).size();
            if(SelectData.SelectType==1)
            {
                cout<<"count:"<<ct<<"\n";
                return;
            }
            if(SelectData.SelectType==2)
            {
                for(vector<int>::iterator iit = (*slist[0]).begin(); iit!=(*slist[0]).end(); iit++)
                 {
                     if(sdata[0][*iit] != NULL)
                        sum = sum + *((int*)sdata[0][*iit]);
                 }
                cout<<"sum:"<<sum<<"\n";
                return;
            }

            for(vector<int>::iterator iit = (*slist[0]).begin(); iit!=(*slist[0]).end(); iit++)
            {
                for(i=0; i<ssize[0]; i++)
                {
                    if(attrtype[i] == 0)
                    {
                        if(sdata[i][*iit] == NULL)
                            sout<<"NULL"<<" | ";
                        else
                            sout<< *((string*)sdata[i][*iit])<<" | ";
                    }
                    else
                    {
                        if(sdata[i][*iit] == NULL)
                            sout<<"NULL"<<" | ";
                        else
                            sout<< *((int*)sdata[i][*iit])<<" | ";
                    }
                }
                sout<<endl;
            }
        }
        else
        {
            ct = sdata[0].size();
            if(SelectData.SelectType==1)
            {
                cout<<"count:"<<ct<<"\n";
                return;
            }
            if(SelectData.SelectType==2)
            {
                for(j=0; j<ct; j++)
                    if(sdata[0][j] != NULL)
                        sum = sum + *((int*)sdata[0][j]);
                cout<<"sum:"<<sum<<"\n";
                return;
            }
            for(j=0; j<ct; j++)
            {
                for(i=0; i<ssize[0]; i++)
                {
                    if(attrtype[i] == 0)
                    {
                        if(sdata[i][j] == NULL)
                            sout<<"NULL"<<" | ";
                        else
                            sout<< *((string*)sdata[i][j])<<" | ";
                    }
                    else
                    {
                        if(sdata[i][j] == NULL)
                            sout<<"NULL"<<" | ";
                        else
                            sout<< *((int*)sdata[i][j])<<" | ";
                    }
                }
                sout<<endl;
            }
        }
    }
    cout<<"count:"<<ct<<"\n";
    cout<<"sum:"<<sum<<"\n";
};


vector<int>* DBMS::SimpleFind(vector<void*> &data,int type,void* val,int symbol,int ma,int mi)
{
    int i=0;
    int clen = data.size();
    vector<int>* l = new vector<int>;
    if(symbol == 0)
    {
        if(type==0)
        {
            for(i=0; i<clen; i++)
            {
                if(*((string*)data[i]) > *((string*)val) )
                    (*l).push_back(i);
            }
        }
        if(type==1)
        {
        if( *((int*)val) < ma)
        {
            for(i=0; i<clen; i++)
                {
                    if(*((int*)data[i]) >  *((int*)val) )
                        (*l).push_back(i);
                }
        }
        }
    }
    if(symbol == 1)
    {
        if(type==0)
        {
            for(i=0; i<clen; i++)
            {
                if(*((string*)data[i]) <  *((string*)val) )
                    (*l).push_back(i);
            }
        }
        if(type==1)
        {
            //if( *((int*)val) > mi)
            //{
            for(i=0; i<clen; i++)
                {
                    if(*((int*)data[i]) <  *((int*)val) )
                        (*l).push_back(i);
                }
            //}
        }
    }
    if(symbol == 2)
    {
        if(type==0)
        {
            for(i=0; i<clen; i++)
            {
                if(*((string*)data[i]) ==  *((string*)val) )
                    (*l).push_back(i);
            }
        }
        if(type==1)
        {
            for(i=0; i<clen; i++)
            {
                if(*((int*)data[i]) ==  *((int*)val))
                    (*l).push_back(i);
            }
        }
    }
    if(symbol == 3)
    {
        if(type==0)
        {
            for(i=0; i<clen; i++)
            {
                if(*((string*)data[i]) !=  *((string*)val) )
                    (*l).push_back(i);
            }
        }
        if(type==1)
        {
            for(i=0; i<clen; i++)
            {
                if(*((int*)data[i]) !=  *((int*)val))
                    (*l).push_back(i);
            }
        }
    }
    return l;
};

vector<int>*  DBMS::AndOp(vector<int>* l1,vector<int>* l2)
{
    set<int> tmp;
    for (vector<int>::iterator it = (*l1).begin(); it!= (*l1).end(); it++)
        tmp.insert(*it);
    vector<int>* re = new vector<int>;
    for (vector<int>::iterator it = (*l2).begin(); it!= (*l2).end(); it++)
        if (tmp.count(*it))
            (*re).push_back(*it);
    return re;
};

vector<int>*  DBMS::OrOp(vector<int>* l1,vector<int>* l2)
{
    set<int> tmp;
    for (vector<int>::iterator it = (*l1).begin(); it!= (*l1).end(); it++)
        tmp.insert(*it);
    for (vector<int>::iterator it = (*l2).begin(); it!= (*l2).end(); it++)
        tmp.insert(*it);
    vector<int>* re = new vector<int>;
    for (set<int>::iterator iter = tmp.begin(); iter != tmp.end(); iter++)
        (*re).push_back(*iter);
    return re;
};
void DBMS::CreateIndex()
{

    list<Table>::iterator it;
    //ofstream out("name.index");
    for(it = db.begin(); it != db.end(); it++)
    {
        int n = it->GetSize();
        int* type = it->GetType();
        for(int i=0; i<n; i++)
        {
            cout<<"input 1 to creat index for :"<<it->GetName()<<","<<i+1<<"\n";
            int ch=1;
            cin>>ch;
            if(ch ==1 )
            {
                FILE *fp;
                string num = "0";
                num[0] =num[0]+i+1;
                string name = "index/"+(it->GetName())+"_"+num+".index";
                //out<<name;
                fp=fopen(name.c_str(),"w");
                if(type[i]==0)
                {
                    Chash_string* tmp =  Hash_string_CreateIndex(&(*it),i);
                    tmp->saveToFile(fp);
                }
                else
                {
                    Chash_int* tmp = Hash_int_CreateIndex(&(*it),i);
                    tmp->saveToFile(fp);
                }
                fclose(fp);
            }
        }
        //out.close();
    }
};

Chash_int*  DBMS::IndexFindInt(Table* table,int attr_id)
{
    //vector<int>* re=0;
    string num = "0";
    num[0] =num[0]+attr_id+1;
    string name = "index/"+(table->GetName())+"_"+num+".index";
    FILE *fp;
    fp=fopen(name.c_str(),"r");
    Chash_int* tmp=0;
    if(fp)
        //if(symbol==2)
    {
        cout<<"index finded"<<"\n";
        int TypeID;
        fscanf(fp, "%d ", &TypeID);
        /*
        if(type ==0)
        {
            Chash_string tmp;
            tmp.recoverFromFile(fp);
            re=tmp.search(*((string*)val));
        }
        if(type ==1)
        {*/
        tmp = new Chash_int;
        tmp->recoverFromFile(fp);
        //re=tmp.search(*((int*)val));
        //}
        fclose(fp);
    }
    /*
    else
    {
    re = new vector<int>;
     (*re).push_back(-1);
    }*/
    return tmp;
};

